﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Admin.CYT369.Com.WebBase;

namespace user.admin.cyt369.com.Areas.UserAdmin.Controllers
{
    public partial class AppController
    {
        #region App
        [HttpGet]
        public ActionResult GetApps()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;

            string name = Request.Query.GetString("Name");

            
            var items = user.admin.cyt369.com.services.Services.AppService.Instance.GetApps(page, rows, ref total);

            return DefaultJson(new { total = total, rows = items });
        }

        [HttpGet]
        public IActionResult GetAppByID(long id)
        {
            var role = user.admin.cyt369.com.services.Services.AppService.Instance.GetAppByID(id);

            return DefaultJson(role);
        }

        [HttpDelete]
        public IActionResult DeleteApp(long id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (user.admin.cyt369.com.services.Services.AppService.Instance.DeleteApp(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public IActionResult SaveApp([FromForm] user.admin.cyt369.com.services.Models.App app)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            app.CreatedUserID = this.GetUserId();
            app.CreatedTime = DateTime.Now;

            if (app != null)
            {
                if (user.admin.cyt369.com.services.Services.AppService.Instance.SaveApp(app))
                {
                    resp.Msg = app.Id.ToString();
                    resp.Result = true;
                }
                else
                {
                    resp.Msg = "保存失败";
                }
            }
            else
            {
                resp.Msg = "参数错误";
            }

            return DefaultJson(resp);
        }

        #endregion


    }
}